package com.example.mybatisplusdemo.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.Methods.methods;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.*;
import com.example.mybatisplusdemo.pojo.Admin;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api")
@CrossOrigin
@Validated
public class AdminController {
    @Resource
    AdminService adminService;
    @Resource
    BusinessService businessService;
    @Resource
    UserService userService;
    @Resource
    GoodService goodService;
    @Resource
    OrderService orderService;


    /**
     * 获取admin列表
     * @param token
     * @return
     */
    @GetMapping("/getadmin")
    public Result getadmin(@RequestParam("token") String token){
        if (!adminService.checktoken(token)){
            return new Result(400,"无权限");
        }
        List<Admin> list = adminService.list(new QueryWrapper<Admin>().orderByDesc("id"));
        return new Result(200,"获取成功",list);
    }

    /**
     * 按名查找 User
     * @param token
     * @param words
     * @return
     */
    @GetMapping("/getadminbyname")
    public Result getadminbyname(@RequestParam("token") String token,
                                 @RequestParam("words") String words){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        List<Admin> list = adminService.list(new QueryWrapper<Admin>().like("nike",words).orderByDesc("id"));
        return new Result(200,"查询成功",list);
    }

    /**
     * 添加admin
     * @param token
     * @param nike
     * @param loginname
     * @param pwd
     * @param tel
     * @return
     */
    @PostMapping("/addadmin")
    public Result addadmin(@Valid @NotBlank(message = "token不能为空") @RequestParam("token") String token,
                           @Valid @NotBlank(message = "昵称不能为空") @RequestParam("nike") String nike,
                           @Valid @NotBlank(message = "用户名不能为空") @RequestParam("loginname") String loginname,
                           @Valid @NotBlank(message = "密码不能为空") @RequestParam("pwd") String pwd,
                           @Valid @NotBlank(message = "电话不能为空") @RequestParam("tel") String tel
    ){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        if  (!new methods().checklenth(loginname,7,15)) {return new Result(400,"用户名长度不正确");}
        if  (!new methods().checklenth(pwd,6,15)) {return new Result(400,"密码长度不正确");}
        int count = adminService.count(new QueryWrapper<Admin>().eq("loginname",loginname));
        if (count != 0){
            return new Result(400,"登录账号已经存在！");
        }
        Admin admin = new Admin();
        admin.setNike(nike);
        admin.setLoginname(loginname);
        admin.setPwd(new methods().md5(pwd));
        admin.setTel(tel);
        admin.setToken(new methods().randomstr(14));
        boolean isSave = adminService.save(admin);
        if (isSave){return new Result(200,"添加成功");}
        else{return new Result(400,"添加失败");}
    }


    /**
     * 删除admin
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/deleteadmin")
    public Result deleteuser(@Valid @NotBlank(message = "token不能为空") @RequestParam("token") String token
            ,@Valid @NotBlank(message = "id不能为空") @RequestParam("id") String id){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        boolean isDelete = adminService.remove(new QueryWrapper<Admin>().eq("id",id));
        if (isDelete){return new Result(200,"删除成功");}
        else{return new Result(400,"删除失败");}
    }

    /**
     * 更新方法
     * @param token
     * @param id
     * @param nike
     * @param tel
     * @return
     */
    @PostMapping("/updateadmin")
    public Result updateadmin(@Valid @NotBlank(message = "token不能为空") @RequestParam("token") String token,
                              @Valid @NotBlank(message = "id不能为空") @RequestParam("id") String id,
                              @Valid @NotBlank(message = "昵称不能为空") @RequestParam("nike") String nike,
                              @Valid @NotBlank(message = "电话不能为空") @RequestParam("tel") String tel){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        if  (!new methods().checklenth(nike,3,10)) {return new Result(400,"昵称长度不正确");}
        Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq("id",id));
        admin.setNike(nike);
        admin.setTel(tel);
        boolean isUpdate = adminService.update(admin,new QueryWrapper<Admin>().eq("id",id));
        if (isUpdate){return new Result(200,"更新成功");}
        else {return new Result(400,"更新失败");}
    }

    /**
     * 重置账号密码
     * @param token
     * @param id
     * @param loginname
     * @param pwd
     * @return
     */
    @PostMapping("/resetadmin")
    public Result resetadmin(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                             @Valid @NotBlank(message = "id不能为空")@RequestParam("id") String id,
                             @Valid @NotBlank(message = "用户名不能为空")@RequestParam("loginname") String loginname,
                             @Valid @NotBlank(message = "密码不能为空")@RequestParam("pwd") String pwd){
        if (!adminService.checktoken(token)){
            return new Result(400,"权限不足");
        }
        Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq("id",id));
        if (!Objects.equals(admin.getLoginname(), loginname)){
            int count = adminService.count(new QueryWrapper<Admin>().eq("loginname",loginname));
            if (count != 0){return new Result(400,"登录账号已经存在");}
            admin.setLoginname(loginname);
        }
        if (!Objects.equals(admin.getPwd(), new methods().md5(pwd))){
            admin.setPwd(new methods().md5(pwd));
        }
        boolean isUpdata = adminService.update(admin,new QueryWrapper<Admin>().eq("id",id));
        if (isUpdata){return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }

    /**
     * 查询所有商家以及用户数量
     * @return
     */
    @GetMapping("/getShopDetail")
    public Result getShopDetail(){
        HashMap<String, Object> res = new HashMap<>();
        res.put("busCount", businessService.count());
        res.put("userCount", userService.count());
        res.put("orderCount", orderService.count());
        res.put("goodCount", goodService.count());
        return new Result(200,"success",res);
    }
}
